/*
 * ChangelogListAdapter.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.changelog;

import de.studiorutton.gerengdic.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChangelogListAdapter extends BaseAdapter {
	private final static String TAG = "learningwords";
	private Changelog mChangelog;
	private IItemsUpdateListener ul = new MyItemsUpdateListener();
	private Context mCtx;
	
	public ChangelogListAdapter( Context ctx, Changelog changelog ) {
		mChangelog = changelog;
		mChangelog.setUpdateListener( ul );
		mCtx = ctx;
	}
	
	public void setContext( Context ctx ) {
		mCtx = ctx;
	}
	
	public int getCount() {
		return mChangelog.getSize();
	}

	public Object getItem(int index) {
		return mChangelog.getRelease( index );
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int index, View convertView, ViewGroup parent) {
		LinearLayout ll;
		if (convertView == null) {
			ll = (LinearLayout) LayoutInflater.from( mCtx ).inflate(
					R.layout.changelog_release, parent, false );
		} else {
			ll = (LinearLayout) convertView;
		}
		
		Release r = mChangelog.getRelease( index );
		TextView tvVersion = (TextView) ll.findViewById( R.id.release_version );
		tvVersion.setText( r.getVersion());
		
		int crrItem = 0;
		while( crrItem < r.size()) {
			TextView tvItem;
			LinearLayout llChild;
			if ( crrItem < ll.getChildCount() - 1) {
				llChild = (LinearLayout) ll.getChildAt( crrItem + 1 );
			} else {
				llChild = (LinearLayout) LayoutInflater.from( mCtx ).inflate( R.layout.changelog_release_item, ll, false );
				ll.addView( llChild );
			}
			tvItem = (TextView) llChild.findViewById( R.id.release_item );
			tvItem.setText( "●" + r.getItem( crrItem ));
			
			crrItem++;
		}
		
		int crrChild = ll.getChildCount();
		while ( crrChild > r.size() + 1) {
			ll.removeViewAt( crrChild - 1 );
			crrChild--;
		}
		
		return ll;
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	class MyItemsUpdateListener implements IItemsUpdateListener {

		public void notifyUpdate() {
			notifyDataSetChanged();
		}
		
	}

}
